package com.photoeditor.demo.db;

import android.graphics.PointF;
import android.graphics.Typeface;
import android.text.Layout;

/**
 * 视频文字
 * Created by zhangwei on 19-6-25.
 */
public class SubtitleAnimBean2 {

    private CharSequence text;
    private float textSize;
    private int textColor;
    private Typeface textFont;

//    private AnimatorSet enterAnimSet;
//    private Animator.AnimatorListener enterAnimListener;

    private long startPositionMs;
    private long endPositionMs = -1;

    private int fromX;
    private int toX;
    private int fromY;
    private int toY;
    private int xDelta;
    private int yDelta;
    private PointF currentPosition;
    public boolean withTranslateAnim;

    private float fromAlpha;
    private float toAlpha;
    private float alphaDelta;
    private float currentAlpha = 1f;
    private boolean withAlphaAnim;

    private float fromScale;
    private float toScale;
    private float scaleDelta;
    private float currentScale = 1f;
    private boolean withScaleAnim;

    private Layout.Alignment mAlignment = Layout.Alignment.ALIGN_NORMAL;

    private boolean mIsAnim = true;

    private int mMaxWidth = -1;

    /**
     * 固定位置的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param x
     * @param y
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int x, int y) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        this.currentPosition = new PointF(x, y);
    }

    /**
     * 只有位移效果的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int fromX, int toX, int fromY, int toY) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initTranslate(fromX, toX, fromY, toY);
    }

    /**
     * 只有透明变化效果的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param x
     * @param y
     * @param fromAlpha
     * @param toAlpha
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int x, int y, float fromAlpha, float toAlpha) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initAlpha(fromAlpha, toAlpha);
    }

    /**
     * 只有缩放变化效果的文字
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont,long startPositionMs, long endPositionMs,float fromScale, float toScale) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initScale(fromScale,toScale);
    }

    /**
     * 位移和透明度变化效果的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     * @param fromAlpha
     * @param toAlpha
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int fromX, int toX, int fromY, int toY, float fromAlpha, float toAlpha) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initTranslate(fromX, toX, fromY, toY);
        initAlpha(fromAlpha, toAlpha);
    }

    /**
     * 位移和缩放变化效果的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     * @param fromScale
     * @param toScale
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int fromX, int toX, int fromY, int toY, float fromScale, float toScale,float nu) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initTranslate(fromX, toX, fromY, toY);
        initScale(fromScale, toScale);
    }

    /**
     * 位移和透明度和缩放变化效果的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param fromX
     * @param toX
     * @param fromY
     * @param toY
     * @param fromAlpha
     * @param toAlpha
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int fromX, int toX, int fromY, int toY, float fromAlpha, float toAlpha, float fromScale, float toScale) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initTranslate(fromX, toX, fromY, toY);
        initAlpha(fromAlpha, toAlpha);
        initScale(fromScale, toScale);
    }

    /**
     * 透明度和缩放效果的文字
     * @param text
     * @param textSize
     * @param textColor
     * @param textFont
     * @param startPositionMs 请注意是在当前播放视频段的开始位置，例如当前是频段是10～20秒，动画在原完整视频的开始时间为15秒，则当前值为：15-10=5*1000L毫秒
     * @param endPositionMs 赋值时注意点同startPositionMs
     * @param x
     * @param y
     * @param fromAlpha
     * @param toAlpha
     * @param fromScale
     * @param toScale
     */
    public SubtitleAnimBean2(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMs, long endPositionMs, int x, int y,
                             float fromAlpha, float toAlpha,
                             float fromScale, float toScale) {
        initText(text, textSize, textColor, textFont, startPositionMs, endPositionMs);
        initAlpha(fromAlpha, toAlpha);
        initScale(fromScale, toScale);
    }

    private void initText(CharSequence text, float textSize, int textColor, Typeface textFont, long startPositionMills, long endPositionMills) {
        this.text = text;
        this.textSize = textSize;
        this.textColor = textColor;
        this.textFont = textFont;
        this.startPositionMs = startPositionMills;
        this.endPositionMs = endPositionMills;
    }

    private void initTranslate(int fromX, int toX, int fromY, int toY) {
        this.fromX = fromX;
        this.toX = toX;
        this.fromY = fromY;
        this.toY = toY;
        this.xDelta = toX - fromX;
        this.yDelta = toY - fromY;
        this.currentPosition = new PointF(fromX, fromY);
        this.withTranslateAnim = true;
    }

    private void initAlpha(float fromAlpha, float toAlpha) {
        this.fromAlpha = fromAlpha;
        this.toAlpha = toAlpha;
        this.alphaDelta = toAlpha - fromAlpha;
        this.currentAlpha = fromAlpha;
        this.withAlphaAnim = true;
    }

    private void initScale(float fromScale, float toScale) {
        this.fromScale = fromScale;
        this.toScale = toScale;
        this.scaleDelta = toScale - fromScale;
        this.withScaleAnim = true;
    }

    public long getStartPositionMs() {
        return startPositionMs;
    }

    public long getEndPositionMs() {
        return endPositionMs;
    }

    public CharSequence getText() {
        return text;
    }

    public float getTextSize() {
        return textSize;
    }

    public int getTextColor() {
        return textColor;
    }

    public Typeface getTextFont() {
        return textFont;
    }

    /**
     * 获取当前位移位置
     * @param currentVideoPositionMs
     * @param videoDurationMs
     * @return null 代表视频播放进度超过动画结束时间
     */
    public PointF getCurrentPosition(long currentVideoPositionMs, long videoDurationMs) {
//        if (!withTranslateAnim || currentVideoPositionMs < startPositionMs) {
//            return currentPosition;
//        }
//
//        if (currentVideoPositionMs > endPositionMs) {
//            return null;
//        }

        if (!withTranslateAnim) {
            return currentPosition;
        }

        if (videoDurationMs == 0L) {
            return currentPosition;
        }

        float percent = (float) (currentVideoPositionMs - startPositionMs) / (endPositionMs - startPositionMs);
        float x = fromX + percent * xDelta;
        float y = fromY + percent * yDelta;
        currentPosition = new PointF(x, y);

        return currentPosition;
    }

    /**
     * 获取当前alpha
     * @param currentVideoPositionMs
     * @param videoDurationMs
     * @return -1 代表视频播放进度超过动画结束时间
     */
    public float getCurrentAlpha(long currentVideoPositionMs, long videoDurationMs) {
//        if (!withTranslateAnim || currentVideoPositionMs < startPositionMs) {
//            return currentAlpha;
//        }

//        if (currentVideoPositionMs > endPositionMs) {
//            return -1f;
//        }

        if (!withAlphaAnim) {
            return currentAlpha;
        }

        if (videoDurationMs == 0L) {
            return currentAlpha;
        }
        float percent = (float) (currentVideoPositionMs - startPositionMs) / (endPositionMs - startPositionMs);

        return fromAlpha + percent * alphaDelta;
    }

    /**
     * 获取当前scale
     * @param currentVideoPositionMs
     * @param videoDurationMs
     * @return -1 代表视频播放进度超过动画结束时间
     */
    public float getCurrentScale(long currentVideoPositionMs, long videoDurationMs) {
//        if (!withTranslateAnim || currentVideoPositionMs < startPositionMs) {
//            return currentScale;
//        }
//
//        if (currentVideoPositionMs > endPositionMs) {
//            return -1f;
//        }

        if (!withScaleAnim) {
            return currentScale;
        }

        if (videoDurationMs == 0L) {
            return currentScale;
        }

        float percent = (float) (currentVideoPositionMs - startPositionMs) / (endPositionMs - startPositionMs);

        return fromScale + percent * scaleDelta;
    }

    public Layout.Alignment getAlignment() {
        return mAlignment;
    }

    public void setAlignment(Layout.Alignment alignment) {
        mAlignment = alignment;
    }

    public boolean isAnim() {
        return mIsAnim;
    }

    public void setAnim(boolean anim) {
        mIsAnim = anim;
    }

    public int getMaxWidth() {
        return mMaxWidth;
    }

    public void setMaxWidth(int maxWidth) {
        mMaxWidth = maxWidth;
    }

}
